﻿using Sidvall.Serialization;
using System;
using Windows.UI.Xaml.Data;

namespace Sidvall.UI.Uwp.Converters
{
    public abstract class ValueToBooleanConverterBase : IValueConverter
    {
        #region Public Members

        protected abstract object BooleanToValue(object value, Type targetType, object parameter, string language);

        #region Inverted

        private bool _Inverted = false;
        public bool Inverted
        {
            get
            {
                return _Inverted;
            }
            set
            {
                _Inverted = value;
            }
        }

        #endregion
        #region Not

        private bool _Not = false;
        public bool Not
        {
            get
            {
                return _Not;
            }
            set
            {
                _Not = value;
            }
        }

        #endregion

        #region Convert

        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (!this.Inverted)
                return ValueToBoolean(value, targetType, parameter, language);
            else
                return BooleanToValue(value, targetType, parameter, language);
        }

        #endregion
        #region ConvertBack

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            if (!this.Inverted)
                return BooleanToValue(value, targetType, parameter, language);
            else
                return ValueToBoolean(value, targetType, parameter, language);
        }

        #endregion

        #region ValueToBoolean

        protected virtual bool ValueToBoolean(object value, Type targetType, object parameter, string language)
        {
            if (value.ToString(string.Empty).Equals(parameter.ToString(string.Empty), true, System.StringComparison.OrdinalIgnoreCase))
            {
                if (!this.Not)
                    return true;
                else
                    return false;
            }
            else
            {
                if (!this.Not)
                    return false;
                else
                    return true;
            }
        }

        #endregion

        #endregion
    }
}
